package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    CategoryService categoryService;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {//套餐分页查询
        //设置分页参数
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        //查询参数
        List page = setmealMapper.page(setmealPageQueryDTO);
        //封装返回
        Page pageResult = (Page) page;
        return new PageResult(pageResult.getTotal(), pageResult.getResult());

    }

    @Override
    public void save(SetmealDTO setmealDTO) { //套餐新增
        //补全信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        setmealMapper.insertSetmealDishes(setmeal.getId(),setmealDTO.getSetmealDishes());
    }
    //
    @Override
    public SetmealVO getById(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        //补充套餐中的分类名称，菜品列表
        Category category =categoryService.getById(setmeal.getCategoryId());
        setmealVO.setCategoryName(category.getName());
        setmealVO.setSetmealDishes(dishMapper.listBySetmealId(id));
        return setmealVO;
    }
    //修改套餐
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        //修改套餐表setmeal
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);
        //修改套餐菜品关系表setmeal_dish
        //先删除原套餐菜品关系数据
        setmealMapper.deleteSetmealDishesBySetmealId(Arrays.asList(setmealDTO.getId()));
        //再添加新的套餐菜品关系数据
        setmealMapper.insertSetmealDishes(setmeal.getId(),setmealDTO.getSetmealDishes());

    }

    //套餐批量删除
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //先删除套餐表setmeal中的数据
        setmealMapper.delete(ids);
        //再删除对应的套餐菜品关系表setmeal_dish中的数据
        setmealMapper.deleteSetmealDishesBySetmealId(ids);

    }
    //套餐起售停售
    @Override
    public void permission(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.permission(setmeal);

    }
}
